using System;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Sedna.Core.Globalization;

namespace Sedna.Core.Controller.Configurations
{
	public class ImageDataTranslator : IDataTranslator
	{
		private IDictionary value2image;
        private IDictionary value2tooltip;
        private string defaultImage;
        private string defaultTooltip;
        private IImageManager imageManager;
		private Type enumType;
        private String labelKey;
        private IMessageManager messageManager;

		public IDictionary Value2image
		{
			get { return value2image; }
			set { value2image = value; }
		}

        public IDictionary Value2tooltip
        {
            get { return value2tooltip; }
            set { value2tooltip = value; }
        }

	    public string DefaultImage
		{
			get { return defaultImage; }
			set { defaultImage = value; }
		}

	    public string DefaultTooltip
	    {
	        get { return defaultTooltip; }
	        set { defaultTooltip = value; }
	    }

	    public IImageManager ImageManager
		{
			get { return imageManager; }
			set { imageManager = value; }
		}

		public Type EnumType
		{
			get { return enumType; }
			set { enumType = value; }
		}

	    public string LabelKey
	    {
	        get { return labelKey; }
	        set { labelKey = value; }
	    }

	    public IMessageManager MessageManager
	    {
	        get { return messageManager; }
	        set { messageManager = value; }
	    }

	    public object Translate(Control control, object value)
		{
			return Translate(control, value, null);
		}

		public object Translate(Control control, object value, Type targetType)
		{
			if (enumType != null && !Enum.IsDefined(enumType, value))
			{
				return CreateImageTag(control, defaultImage, defaultTooltip);
			}

            return CreateImageTag(control, GetPictureName(value), GetTooltip(value));
		}

		private string CreateImageTag(Control control, string imageName, string toolTipKey)
		{
            if (messageManager != null)
    		    return String.Format("<img src=\"{0}\" title=\"{1}\"/>", HttpUtility.HtmlAttributeEncode(control.ResolveUrl(imageManager.RelateImageUrl(imageName))),
                    GetLocalizedPostfixLabel(toolTipKey));
		    else
                return
                    String.Format("<img src=\"{0}\" />",
                                  HttpUtility.HtmlAttributeEncode(
                                      control.ResolveUrl(imageManager.RelateImageUrl(imageName))));
		}

		private string GetPictureName(object value)
		{
			if(value2image == null)
			{
				return defaultImage;
			}
			
			string imageName = (string)value2image[GetStringValue(value)];
			
			return imageName!=null?imageName:defaultImage;
		}

        private string GetTooltip(object value)
        {
            if (value2tooltip == null)
            {
                return defaultTooltip;
            }

            string tooltip = (string)value2tooltip[GetStringValue(value)];

            return tooltip != null ? tooltip : defaultTooltip;
        }

	    private string GetStringValue(object value)
		{
			if(value == null || String.Empty.Equals(value))
			{
				return string.Empty;
			}
			if(enumType == null)
			{
				return value.ToString();
			}
			return Enum.GetName(enumType, value);
        }

        #region Localization

        public string GetLocalizedPostfixLabel(string postfix)
        {
            return MessageManager.GetMessage(LabelKey + "." + postfix, String.Empty);
        }

        #endregion
    }
}
